﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace V5_Common
{
    public class Input
    {
        public static string SetString(object o)
        {
            return Convert.ToString("" + o);
        }

        /// <summary>
        /// 转化成int类型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int SetInt(object str)
        {
            return int.Parse("0" + str);
        }

        /// <summary>
        /// md5加密方式
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="code">16，32 位</param>
        /// <returns>加密后的字符串</returns>
        public static string MD5(string str, int code)
        {
            if (code == 16)
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToLower().Substring(8, 16);
            }
            if (code == 32)
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToUpper();
            }
            return str;
        }

        // 返回指定字符串的值
        public static string TagVal(string Tag, string TagName)
        {
            string[] strArray = Tag.Split(new char[] { ',' });
            for (int i = 0; i < strArray.Length; i++)
            {
                Regex regex = new Regex(@"(?<Keyword>\w+)\s*=\s*(?<Value>.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                for (Match match = regex.Match(strArray[i]); match.Success; match = match.NextMatch())
                {
                    if (match.Groups["Keyword"].ToString().ToLower().IndexOf(TagName.ToLower()) != -1)
                    {
                        return match.Groups["Value"].ToString().ToLower();
                    }
                }
            }
            return "";
        }

        //截取函数
        public static string CutString(string str, int length,string addstr)
        {
            int i = 0, j = 0;
            foreach (char chr in str)
            {
                i += 2;
                if (i > length)
                {
                    str = str.Substring(0, j - 1) ;
                    if (addstr != "")
                    {
                        str += addstr;
                    }
                    break;
                }
                j++;
            }
            return str;
        }

        /// <summary>
        ///SQL注入过滤
        /// </summary>
        /// <param name="InText">要过滤的字符串</param>
        /// <returns>如果参数存在不安全字符，则返回true</returns>
        public static bool SqlFilter(string InText)
        {
            string word = "and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join";
            if (InText == null)
                return false;
            foreach (string i in word.Split('|'))
            {
                if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
